package com.example.spider.testing;

import cn.hutool.core.io.FileUtil;
import com.example.spider.util.HeaderUtil;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

public class ImageUtils {

    public static OkHttpClient client = new OkHttpClient().newBuilder()
            .connectionPool(new ConnectionPool(5, 10L, TimeUnit.SECONDS))
            .build();

    public static void zoomInImage(String srcPath, String newPath, int maxWidth, int maxHeight) throws Exception {
        BufferedImage bufferedImage = null;
        File file = new File(srcPath);
        bufferedImage = ImageIO.read(file);
        if (bufferedImage != null) {
            bufferedImage = zoomInImage(bufferedImage, maxWidth, maxHeight);
        }
    }

    public static BufferedImage zoomInImage(BufferedImage originalImage, int maxWidth, int maxHeight) {
        BufferedImage newImage = new BufferedImage(maxWidth, maxHeight, originalImage.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0, 0, maxWidth, maxHeight, null);
        g.dispose();
        return newImage;
    }

    public static BufferedImage convertToBufferedImage(Image image) {
        BufferedImage newImage = new BufferedImage(80, 80, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = newImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return newImage;
    }

    public static void main(String[] args) throws MimeTypeException, IOException {
        String url = "https://proxy.mimvp.com/common/ygrandimg?id=1&port=MmDihmtvapW12cDgwODAO0O";
        downloadImage(url, "F:\\spider\\xxx");
    }

    public static File downloadImage(String url, String aimFile) throws IOException, MimeTypeException {
        // 请求
        Request request = new Request.Builder()
                .url(url)
                .method("GET", null)
                .addHeader("authority", "proxy.mimvp.com")
                .addHeader("pragma", "no-cache")
                .addHeader("cache-control", "no-cache")
                .addHeader("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Microsoft Edge\";v=\"99\"")
                .addHeader("sec-ch-ua-mobile", "?0")
                .addHeader("sec-ch-ua-platform", "\"Windows\"")
                .addHeader("upgrade-insecure-requests", "1")
                .addHeader("user-agent", HeaderUtil.randomUserAgent())
                .addHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                .addHeader("sec-fetch-site", "none")
                .addHeader("sec-fetch-mode", "navigate")
                .addHeader("sec-fetch-user", "?1")
                .addHeader("sec-fetch-dest", "document")
                .addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                //.addHeader("cookie", "MIMVPSESSID=jjlqthl8oi2098jnn9sq149prm; Hm_lvt_51e3cc975b346e7705d8c255164036b3=1648404481,1648833428; Hm_lpvt_51e3cc975b346e7705d8c255164036b3=1648833428")
                .build();
        Response response = client.newCall(request).execute();

        File file = new File(aimFile);
        String fName = file.getName();
        String ext;
        if (!fName.contains(".")) {
            // 解析扩展名
            String contentType = response.header("content-type");
            MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
            MimeType jpeg = allTypes.forName(contentType);
            ext = jpeg.getExtension(); //.jpg .png
            file = FileUtil.touch(aimFile + ext);
            ext = ext.substring(1);
        } else {
            ext = fName.substring(fName.lastIndexOf("."));
        }

        assert response.body() != null;
        try (InputStream inputStream = response.body().byteStream()) {
            FileUtil.writeFromStream(inputStream, file);
            return file;
        }
    }


}